<?php
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */
namespace Data;

use Utils\MyException;

class Tree_admin_user_DAO {

    public static $TABLENAME = 'Tree_admin_user';

    public static $_db;

    public static function query($str)
    {
        if(self::$_db === null) {
            self::$_db = \GatewayWorker\Lib\Db::instance('main');
            return self::$_db->query($str);
        } else {
            return self::$_db->query($str);
        }
    }




    public static function getRecord($ID){
        $where = "`ID` = '$ID'";
        $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
        if($record != null){
            return new Tree_admin_user($record[0]);
        }
        return null;
    }

    public static function getRecordByName($name){
        $where = "`NAME` = '$name'";
        $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
        if($record != null){
            return new Tree_admin_user($record[0]);
        }
        return null;
    }



    public static function getList() {
        $where = "";
        $list = self::query('select * from '.static::$TABLENAME);
        $results = array();

        foreach($list as $record){
            $results[] = new Tree_admin_user($record);
        }
        return $results;
    }

    public static function getListByNeedle($needle) {
        $where = "`NAME` like '%$needle%'";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();

        foreach($list as $record){
            $results[] = new Tree_admin_user($record);
        }
        return $results;
    }



    public static function insert($record){

        try {

            $ID = $record->getID();
            $oldrecord = self::getRecord($ID);
            if($oldrecord != null) {
                throw new MyException ("重复插入");
            }
            $sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')' ;
            self::query($sql);
            return self::$_db->lastInsertId();
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function update($record){
        try{
            $ID = $record->getID();
            $where = "`ID` = '$ID'";
            $oldrecord = self::getRecord($ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'update '.static::$TABLENAME .' set '.$record->toUpdateSet().' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function delete ($record){
        try{
            $ID = $record->getID();
            $where = "`ID` = '$ID'";
            $oldrecord = self::getRecord($ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'delete from '.static::$TABLENAME . ' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }
}
